/*
 * Android-PercentProgressBar
 * Copyright (c) 2015  Natasa Misic
 *
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.natasa.progresspercent.lib;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * 线性进度条
 *
 * @since 2021-07-14
 */
public class LineProgress extends BaseProgressView implements Component.DrawTask, Component.EstimateSizeListener {
    private RectFloat rectP;
    private float offBottom = 0.1f;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public LineProgress(Context context) {
        super(context);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 属性集
     */
    public LineProgress(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 属性集
     * @param styleName 样式
     */
    public LineProgress(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    protected void init(Context context) {
        this.mContext = context;
        progress = 0;
        initBackgroundColor();
        initForegroundColor();
        initTextColor();
        rectP = new RectFloat();
        addDrawTask(this);
        setEstimateSizeListener(this);
    }

    @Override
    protected void initForegroundColor() {
        super.initForegroundColor();
    }

    @Override
    protected void initTextColor() {
        super.initTextColor();
    }

    @Override
    protected void initBackgroundColor() {
        super.initBackgroundColor();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        drawRects(canvas);
    }

    private void drawRects(Canvas canvas) {
        float nMiddle = (float) height / 2;
        String text = "" + progress + "%";
        float mt = textPaint.measureText(text) + 40.0f + text.length();
        float progressX = (getWidth() - padding - mt) * progress / maxProgress;
        rectP.left = getPaddingLeft();
        rectP.top = nMiddle;
        rectP.right = progressX;
        rectP.bottom = nMiddle + 0.1f;
        if (progress > 2) {
            canvas.drawRect(rectP, foregroundPaint);
        }
        if (progress < maxProgress) {
            canvas.drawRect(rectP.getWidth() + mt, nMiddle, getWidth() - padding, nMiddle + offBottom, backgroundPaint);
        }
        canvas.drawText(textPaint, text, progressX + 10, nMiddle + backgroundStrokeWidth);
    }

    private void onSizeChanged(int widthMeasureSpec, int heightMeasureSpec) {
        float xpad = (float) (getPaddingLeft() + getPaddingRight());
        float ypad = (float) (getPaddingTop() + getPaddingBottom());
        int widthMode = EstimateSpec.getMode(widthMeasureSpec);
        int heightMode = EstimateSpec.getMode(heightMeasureSpec);
        int widthSize = EstimateSpec.getSize(widthMeasureSpec);
        int heightSize = EstimateSpec.getSize(heightMeasureSpec);
        width = (int) (widthSize - xpad);
        height = (int) (heightSize - ypad);
        setEstimatedSize(Component.EstimateSpec.getChildSizeWithMode(width, width, widthMode),
            Component.EstimateSpec.getChildSizeWithMode(height, height, heightMode));
    }

    private void setPadding() {
        setPadding((int) padding, 0, 0, 0);
    }

    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
        setPadding();
        onSizeChanged(widthMeasureSpec, heightMeasureSpec);
        return true;
    }
}
